<?php


namespace App\PubApi\Controller;

use App\Facades\Captcha;
use App\Facades\JWTAuth;
use App\Facades\UrlCrypt;
use App\PubApi\Model\VideoInterview as VideoModel;
use App\Service\ImgCaptcha;
use App\Web\Model\VideoInterview as VideoInterviewModel;
use EchoPHP\Http\Request;
use EchoPHP\Http\Response\StreamedResponse;

class VideoInterview extends BaseController {

    /**
     * 小程序登录
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function login(Request $request) {
        $code = $request->getParam('code', '');
        $videoModel = new VideoModel();
        $loginInfo = $videoModel->getLoginInfo($code);
        if (empty($loginInfo)) {
            return $this->json(['error' => -1, 'message' => '系统繁忙，请稍后再试']);
        }

        $authToken = JWTAuth::encode($loginInfo, 28800);
        //获取小程序是否绑定rpo系统用户手机号
        $videoModel = new VideoModel();
        $userInfo = $videoModel->getUserInfoByOpenId($loginInfo['openid']);
        $bindMp = isset($userInfo['mobilephone']) ? 1 : 0;
        return $this->json(['error' => 0, 'message' => '登录成功', 'xcxToken' => $authToken, 'bindMp' => $bindMp]);
    }

    /**
     * 获取视频面试微信小程序websocket连接地址
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getLineUrl() {
        $xcxwsUrl = config('extraapi.vsapi.xcxws_url', 'wss://linews.51job.com');
        return $this->json(['error' => 0, 'message' => '', 'url' => $xcxwsUrl]);
    }

    /**
     * 获取并输出图形验证码
     * @param Request $request
     * @return StreamedResponse
     * @throws \Exception
     */
    public function getImgCaptcha(Request $request) {
        $uuid = $request->getParam('uuid');
        $action = $request->getParam('act', 'bind');
        $uuid .= '_' . $action;
        $result = Captcha::generate('image', $uuid);

        $imgCaptcha = new ImgCaptcha();
        //输出普通复杂度的图片
        $options = [
            'level' => 1,
            'type' => 'img'
        ];
        return $imgCaptcha->response($result['code'], $options);
    }

    /**
     * 获取手机验证码
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getMpCode(Request $request) {
        $loginInfo = $this->getXcxLoginInfo();
        if (empty($loginInfo)) {
            return $this->json(['error' => -511, 'message' => '小程序尚未登录']);
        }
        $uuid = $request->getParam('uuid');
        $telPhone = $request->getParam('telphone');
        $verifyCode = $request->getParam('verifycode');

        $videoModel = new VideoModel();
        $result = $videoModel->sendMpCode($uuid, $verifyCode, $telPhone);
        return $this->json($result);
    }

    /**
     * 绑定手机号
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function bind(Request $request) {
        $loginInfo = $this->getXcxLoginInfo();
        if (empty($loginInfo)) {
            return $this->json(['error' => -511, 'message' => '小程序尚未登录']);
        }
        $telPhone = $request->getParam('telphone');
        $verifyCode = $request->getParam('verifycode');
        $videoModel = new VideoModel();
        $result = $videoModel->bindUserInfo($loginInfo['openid'], $telPhone, $verifyCode);
        return $this->json($result);
    }

    /**
     * 解绑用户
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function unbind(Request $request) {
        $loginInfo = $this->getXcxLoginInfo();
        if (empty($loginInfo)) {
            return $this->json(['error' => -511, 'message' => '小程序尚未登录']);
        }
        $videoModel = new VideoModel();
        $result = $videoModel->unbindUserInfo($loginInfo['openid']);
        return $this->json($result);
    }

    /**
     * 获取房间列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getRoomList(Request $request) {
        $loginInfo = $this->getXcxLoginInfo();
        if (empty($loginInfo)) {
            return $this->json(['error' => -511, 'message' => '小程序尚未登录']);
        }
        $videoModel = new VideoModel();
        $userInfo = $videoModel->getUserInfoByOpenId($loginInfo['openid']);
        if (empty($userInfo)) {
            return $this->json(['error' => -2, 'message' => '您尚未绑定手机号']);
        }

        $userType = $request->getParam('usertype', 0);
        $roomList = $videoModel->getRoomListByPhone($userInfo['mobilephone'], $userType);
        return $this->json(['error' => 0, 'message' => '获取成功', 'roomList' => $roomList]);
    }

    /**
     * 获取播放列表实际地址
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function videoRealUrl(Request $request) {
        $type = $request->getPost('type', 2);
        $id = $request->getPost('id', '');
        $token = $request->getPost('token', '');
        $params = [
            'id' => $id
        ];
        if (!UrlCrypt::checkUrlToken($params, $token)) {
            return $this->json(['error' => '-1', 'errormsg' => '您的播放地址不正确或者已过期，请联系顾问获取最新地址']);
        }
        $interviewModel = new VideoInterviewModel();
        if ($type == 2) {
            $videoList = $interviewModel->getVideoPathByUserId($id);
        } else {
            $videoList = $interviewModel->getVideoPathById($id);
        }
        $appid = config('extraapi.vsapi.appid', '');

        return $this->json(['error' => '0', 'errormsg' => '', 'videolist' => $videoList, 'appid' => $appid]);
    }

}